<template>
  <div class="container">
    <video ref="videoRef" class="camera"></video>
  </div>
</template>

<script setup>
import { nextTick, onBeforeUnmount, onMounted, ref } from "vue";

const props = defineProps({
  url: {
    type: String,
    require: true,
  },
});

const videoRef = ref(null);
let webRtcServer = null;
const streamerUrl = `${location.protocol}//${location.hostname}:8000`;

onMounted(() => {
  nextTick(() => {
    webRtcServer = new WebRtcStreamer(videoRef.value, streamerUrl);
    webRtcServer.connect(props.url);
  });
});

onBeforeUnmount(() => {
  webRtcServer.disconnect();
});
</script>

<style scoped>
.container {
  width: 100%;
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
}

.camera {
  padding: 10px;
  max-width: 640px;
  max-height: 480px;
}
</style>
